package com.cskaoyan.service.market;

import com.cskaoyan.bean.market.Keyword;
import com.cskaoyan.bean.market.KeywordExample;
import com.cskaoyan.bo.market.KeywordCreateBo;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.utils.Converter;
import com.cskaoyan.utils.PojoUtils;
import com.cskaoyan.vo.market.KeywordCreateVo;
import com.cskaoyan.vo.market.KeywordListDataVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-09 23:09
 **/

// 关键词
@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    KeywordMapper keywordMapper;


    @Override
    public KeywordListDataVo keywordList(Integer page, Integer limit, String sort, String order, String keyword, String url) {
        // 分页，排序
        PageHelper.startPage(page, limit);

        // 查询关键词数据
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort + " " + order);
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        // 条件查询
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null && !"".equals(url)) {
            criteria.andUrlLike("%" + url + "%");
        }
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        PageInfo<Keyword> issuePageInfo = new PageInfo<>(keywords);
        long total = issuePageInfo.getTotal();

        return new KeywordListDataVo(total,keywords);
    }

    @Override
    public KeywordCreateVo createKeyword(KeywordCreateBo bo) {
        // 要添加的数据
        Keyword keyword = new Keyword(null, bo.getKeyword(), bo.getUrl(), bo.getIsHot(), bo.getIsDefault(),
                100, new Date(), new Date(), false);
        keywordMapper.insert(keyword);
        // 类型转换
        KeywordCreateVo keywordCreateVo = new KeywordCreateVo();
        Converter converter = PojoUtils.converter();
        converter.converter(keyword,keywordCreateVo );

        return keywordCreateVo;
    }

    @Override
    public KeywordCreateVo updateKeyword(KeywordCreateVo keywordCreateVo) {
        // 类型转换
        Keyword keyword = new Keyword();
        Converter converter = PojoUtils.converter();
        converter.converter(keywordCreateVo,keyword);
        // 修改数据
        keyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKeySelective(keyword);

        // 类型转换
        KeywordCreateVo vo = new KeywordCreateVo();
        converter.converter(keyword,vo);
        return vo;
    }

    @Override
    public void deleteKeyword(Keyword keyword) {
        keyword.setDeleted(true);
        keyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKeySelective(keyword);
    }
}
